package com.service.rd;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;

import com.dao.rd.RdSupplyDealDao;
import com.dao.rd.RdSupplyDealMmDao;
import com.dao.sys.SysUserMapper;
import com.model.rd.SupplyDealMmModel;
import com.model.rd.SupplyDealModel;
import com.model.rd.SupplyDealVoModel;
import com.model.supply.SupplyUserModel;
import com.model.sys.SysUser;
import com.service.supply.InfoService;

/**
 * 报价管理
 * 
 * @author chenjun
 *
 */
@Service("rdOfferService")
public class OfferService {

	static final Logger logger = LoggerFactory.getLogger(ProgramService.class);

	@Autowired
	InfoService infoService;
	@Autowired
	RdSupplyDealDao supplyOfferMapper;
	@Autowired
	SysUserMapper userMapper;
	@Autowired
	RdSupplyDealMmDao supplyOfferMmMapper;

	/**
	 * 查询报价列表
	 * 
	 * @param map
	 * @return
	 */
	public HttpResult selectOfferList(Map<String, Object> map) {
		EasyuiUtils.addPageNum(map);
		EasyuiUtils.addFilter(map);

//		map.put("inqMode", "线下报价");

		List<SupplyDealModel> purchaseSupplyOffers = supplyOfferMapper.selectList(map);
		int count = supplyOfferMapper.selectListCount(map);
		HttpResult httpResult = new HttpResult();
		httpResult.setRows(purchaseSupplyOffers);
		httpResult.setTotal(count);
		return httpResult;
	}

	/**
	 * 查询报价明细列表
	 * 
	 * @param map
	 * @return
	 */
	public HttpResult selectOfferMmList(String gramId, String infId) {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("gramId", gramId);
		hashMap.put("infId", infId);
		List<SupplyDealMmModel> supplyOfferMms = supplyOfferMmMapper.selectList(hashMap);

		HttpResult httpResult = new HttpResult();
		httpResult.setRows(supplyOfferMms);

		return httpResult;
	}

	/**
	 * 保存报价明细
	 * 
	 * @param map
	 * @return
	 */
	public HttpResult saveOfferMm(SupplyDealVoModel purchaseSupplyOfferVO) {
		if (purchaseSupplyOfferVO == null || purchaseSupplyOfferVO.getSupplyDealMmModels() == null) {
			return HttpResult.error("请求参数为空");
		}
		if (purchaseSupplyOfferVO.getInfFirstNum() == null || purchaseSupplyOfferVO.getInfFirstTotalPrice() == null) {
			return HttpResult.error("报价数量和价格不能为空！");
		}
		if (purchaseSupplyOfferVO.getInfFirstNum().doubleValue() == 0
				|| purchaseSupplyOfferVO.getInfFirstTotalPrice().doubleValue() == 0) {
			return HttpResult.error("报价数量和价格不能为空！");
		}
		if (purchaseSupplyOfferVO.getInfNum() == null || purchaseSupplyOfferVO.getInfLastTotalPrice() == null) {
			return HttpResult.error("成交数量和价格不能为空！");
		}
		if (purchaseSupplyOfferVO.getInfNum().doubleValue() == 0
				|| purchaseSupplyOfferVO.getInfLastTotalPrice().doubleValue() == 0) {
			return HttpResult.error("成交数量和价格不能为空！");
		}
		int result = 0;
		// 主表保存
		result = supplyOfferMapper.update(purchaseSupplyOfferVO);
		// 明细报价保存
		for (SupplyDealMmModel purchaseSupplyOfferMm : purchaseSupplyOfferVO.getSupplyDealMmModels()) {
			if ((result += supplyOfferMmMapper.update(purchaseSupplyOfferMm)) == 0) {
				result += supplyOfferMmMapper.insert(purchaseSupplyOfferMm);
			}
		}
		HttpResult httpResult = new HttpResult();

		if (result > 0) {
			httpResult.setData(purchaseSupplyOfferVO);
			httpResult.setMsg(purchaseSupplyOfferVO.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			return httpResult;
		} else {
			return HttpResult.error(purchaseSupplyOfferVO.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
	}

	/**
	 * 提交报价
	 * 
	 * @param map
	 * @return
	 */
//	public HttpResult commitOffer(PurchaseSupplyOfferVO purchaseSupplyOfferVO) {
//		if (purchaseSupplyOfferVO == null || purchaseSupplyOfferVO.getPurchaseSupplyOfferMms() == null) {
//			return HttpResult.error("请求参数为空");
//		}
//		int result = 0;
//		// 主表保存
//		result = supplyOfferMapper.update(purchaseSupplyOfferVO);
//		// 明细报价保存
//		for (PurchaseSupplyOfferMm purchaseSupplyOfferMm : purchaseSupplyOfferVO.getPurchaseSupplyOfferMms()) {
//			if ((result += supplyOfferMmMapper.updateByPrimaryKey(purchaseSupplyOfferMm)) > 0) {
//				result += supplyOfferMmMapper.insert(purchaseSupplyOfferMm);
//			}
//		}
//
//		if (result > 0) {
//			return HttpResult.ok("保存成功");
//		} else {
//			return HttpResult.error("保存失败");
//		}
//	}

}
